import axios from 'axios'
import Vue from 'vue'

const instance = axios.create({
    baseURL: '/api',
    timeout: 5000
})

instance.interceptors.request.use(config=>{
    const token = localStorage.getItem('token')
    if(token){
        config.headers = config.headers || {}
        config.headers['Authorization'] = token
    }
    return config
},err=>Promise.reject(err))

instance.interceptors.response.use(res=>{
    if(res.data.code !== 200){
        // 统一的网络请求报错处理
        // Vue.use(Element) 就在 Vue的prototype上面添加element相关的API
        Vue.prototype.$message.error(res.data.message)
    }

    return res.data
},err=>Promise.reject(err))

export default instance